package com.example.controller;

import com.example.utils.DelayQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("redis")
@Slf4j
public class DemoController {

    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("add")
    public void add(){
        DelayQueue delayQueue=new DelayQueue(redisTemplate,"queue");
        delayQueue.put("task01",10000L);
        System.out.println("任务1加入到队列at:"+new Date());

        delayQueue.put("task02",10000L);
        System.out.println("任务2加入到队列at:"+new Date());
        delayQueue.start();
    }

    @GetMapping("cancel")
    public void cancel(){
        DelayQueue delayQueue=new DelayQueue(redisTemplate,"queue");
        delayQueue.cancel("task02");
        System.out.println("任务2取消了");
    }

}
